//
//  HBDataSource.m
//  HBLibary
//
//  Created by Rambo on 2018/6/8.
//

#import "HBDataSource.h"
#import "NSString+HBCategory.h"

@interface HBDataSource ()

@property (nonatomic, copy) NSString *identifier;

@property (nonatomic, copy) HBDataSourceHandle dataSourceBlock;

@property (nonatomic, strong) NSMutableArray *dataM;

@end

@implementation HBDataSource

- (instancetype)initWithIdentifier:(NSString *)identifier tableViewDataSource:(HBDataSourceHandle)dataSource{
    if (self = [super init]) {
        if ([NSString hb_isEmpty:identifier]) @throw @"请传入正确的cell标识符";
        self.identifier = identifier;
        self.dataSourceBlock = dataSource;
    }
    return self;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self numberSection];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self numberItemForSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    id cell = [tableView dequeueReusableCellWithIdentifier:self.identifier];
    
    
    if (self.dataSourceBlock) {
        id model = [self getModelWithIndexPath:indexPath];
        self.dataSourceBlock(cell, model, indexPath);
    }
    
    return cell;
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return [self numberSection];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self numberItemForSection:section];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.identifier forIndexPath:indexPath];
    
    if (self.dataSourceBlock) {
        id model = [self getModelWithIndexPath:indexPath];
        self.dataSourceBlock(cell, model, indexPath);
    }
    
    return cell;
}

#pragma mark - 私有方法

- (NSInteger)numberSection{
    if (self.dataM.count > 0) {
        id firstObjc = self.dataM.firstObject;
        if ([firstObjc isKindOfClass:[NSArray class]]) {
            return self.dataM.count;
        }else{
            return 1;
        }
    }
    return 0;
}

- (NSInteger)numberItemForSection:(NSInteger)section{
    if (self.dataM.count > 0) {
        id firstObjc = self.dataM.firstObject;
        if ([firstObjc isKindOfClass:[NSArray class]]) {
            id objc = self.dataM[section];
            return [objc count];
        }else{
            return self.dataM.count;
        }
    }
    return 0;
}

#pragma mark - 公共方法

- (void)addModels:(NSArray*)models{
    if (!models || models.count == 0) return;
    
    [self.dataM addObjectsFromArray:models];
}

- (void)removeAllModel{
    [self.dataM removeAllObjects];
}

- (id)getModelWithIndexPath:(NSIndexPath*)indexPath{
    if ([self.dataM.firstObject isKindOfClass:[NSArray class]]) {
        if (self.dataM.count > indexPath.section && [self.dataM[indexPath.section] count] > indexPath.item) {
            return self.dataM[indexPath.section][indexPath.item];
        }
    }else{
        if (self.dataM.count > indexPath.item) {
            return self.dataM[indexPath.item];
        }
    }
    return nil;
}


- (NSArray*)getAllModels{
    return self.dataM;
}

- (void)removeModel:(id)model{
    if ([self.dataM.firstObject isKindOfClass:[NSArray class]]) {
        BOOL result = false;
        for (NSInteger index = 0 ; index < self.dataM.count; index++) {
            if (result) break;
            NSMutableArray *array = self.dataM[index];
            if ([array containsObject:model]) {
                result = true;
                [array removeObject:model];
                self.dataM[index] = array;
                break;
            }
            
        }
    }else{
        if ([self.dataM containsObject:model]) {
            [self.dataM removeObject:model];
        }
    }
}

- (void)removeModelAtIndexPath:(NSIndexPath*)indexPath{
    if ([self.dataM.firstObject isKindOfClass:[NSArray class]]) {
        
        if (self.dataM.count > indexPath.section) {
            NSMutableArray *array = self.dataM[indexPath.section];
            if (array.count > indexPath.item) {
                [array removeObjectAtIndex:indexPath.item];
                self.dataM[indexPath.section] = array;
            }
        }
    }else{
        if (self.dataM.count > indexPath.item) {
            [self.dataM removeObjectAtIndex:indexPath.item];
        }
    }
}

#pragma mark - 懒加载

- (NSMutableArray *)dataM{
    if (!_dataM) {
        _dataM = [NSMutableArray array];
    }
    return _dataM;
}

@end
